/* eslint-disable react-refresh/only-export-components */
import { createBrowserRouter, redirect } from 'react-router-dom'
import MainLayout from '../layouts/MainLayout'
import Login from '../pages/Login'
import Dashboard from '../pages/Dashboard'
import GlobalError from '../pages/GlobalError'
import { PermissionCode } from '../types'
import { userStore } from '../stores'
import OrderList from '../pages/OrderList'
import AddProject from '../pages/AddProject'
// import MaintainList from '../pages/MaintainList'
// import MaintainDetail from '../pages/MaintainDetail'
// import OrderAdd from '../pages/OrderAdd'
// import MaintainAdd from '../pages/MaintainAdd'
// import MaintainWaiting from '../pages/MaintainWaiting'

const MainLoader = () => {
  if (userStore.isLoggedIn) {
    return { appName: '一管一码管理系统' }
  }
  return redirect('/login')
}

// useAuth hook已移除，直接使用userStore

const authLoader =
  (requiredPermission: PermissionCode | null = null) =>
  async () => {
    // 检查登录状态
    if (!userStore.isLoggedIn) {
      return redirect('/login')
    }

    // 确保用户信息已加载完成
    await userStore.initUserInfo()

    // 已登录，检查权限码
    if (requiredPermission) {
      if (!userStore.checkPermission(requiredPermission)) {
        return redirect('/403')
      }
    }

    return null
  }

const router = createBrowserRouter(
  [
    {
      path: '/',
      element: <MainLayout />,
      loader: MainLoader,
      children: [
        {
          path: 'dashboard',
          element: <Dashboard />,
          loader: authLoader('sp-operate:dashboard'),
        },
        {
          path: 'list',
          element: <OrderList />,
          loader: authLoader('sp-operate:list'),
          // children: [
          //   {
          //     path: 'pre',
          //     element: <OrderPreprocess />,
          //     loader: authLoader('sp-operate:pre'),
          //   },
          //   {
          //     path: 'check',
          //     element: <OrderCheck />,
          //     loader: authLoader('sp-operate:check'),
          //   },
          //   {
          //     path: 'deal',
          //     element: <OrderDeal />,
          //     loader: authLoader('sp-operate:deal'),
          //   },
          // ],
        },
        // {
        //   path: 'mt',
        //   loader: authLoader('sp-operate:mt'),
        //   element: <MaintainList />,
        // },
        // {
        //   path: 'mtwt',
        //   loader: authLoader('sp-operate:mt'),
        //   element: <MaintainWaiting />,
        // },
      ],
    },
    {
      path: '/login',
      element: <Login />,
    },
    {
      path: '/oadd',
      element: <AddProject />,
      loader: authLoader('sp-operate:list'),
    },
    {
      path: '/detail/:id',
      element: <AddProject/>,
      loader: authLoader('sp-operate:list'),
    },
    // {
    //   path: '/mt/detail/:id',
    //   element: <MaintainDetail />,
    //   loader: authLoader('sp-operate:mt'),
    // },
    // {
    //   path: '/mt/add',
    //   element: <MaintainAdd />,
    //   loader: authLoader('sp-operate:mt'),
    // },
    {
      path: '*',
      element: <GlobalError />,
    },
  ],
  {
    basename: '/ygym',
  }
)

export default router
